﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.ExportPhoto;
using MINERP.DAL;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.Common;

namespace MINERP.BLL.ExportPhoto
{
    public class ExportPhotoBLL
    {

        WorkFlowListBLL current = new WorkFlowListBLL();
        public List<ExportTaskInfo> GetAllWaitList()
        {
            int techType = (int)TechType.ExportPhoto;
            List<ExportTaskInfo> list = new List<ExportTaskInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<WorkOrderFlowList> listwofl = db.WorkOrderFlowList.Where(w => w.tb_Tech.TechType == techType && w.Status == 1).ToList();
                foreach (WorkOrderFlowList wofl in listwofl)
                {
                    if (!wofl.orderInfoReference.IsLoaded)
                    {
                        wofl.orderInfoReference.Load();
                    }
                    if (!wofl.orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBagReference.Load();

                    }
                    if (!wofl.orderInfo.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBag.ShopReference.Load();
                    }
                    if (!wofl.orderInfo.photoInfo.IsLoaded)
                    {
                        wofl.orderInfo.photoInfo.Load();
                    }
                    if (wofl.orderInfo.status != 1)
                    {
                        continue;
                    }
                    ExportTaskInfo eti = new ExportTaskInfo();
                    eti.CtmName = wofl.orderInfo.CtmName;
                    eti.OrderBagId = eti.OrderBagid = wofl.orderInfo.Tb_OrderBag.OrderBagId;
                    eti.OrderPath = wofl.orderInfo.OrderPath;
                    eti.OrderProductName = wofl.orderInfo.OrderProductName;
                    eti.ShopName = wofl.orderInfo.Tb_OrderBag.Shop.shopName;
                    eti.PhotoNum = wofl.orderInfo.photoInfo.Count;
                    eti.OrderProductId = wofl.orderInfo.OrderProductId;
                    eti.listPhoto = new List<MINERP.MODEL.FoundationModel.PhotoInfo_Info>();
                    foreach (photoInfo photo in wofl.orderInfo.photoInfo)
                    {
                        PhotoInfo_Info pi = new PhotoInfo_Info();
                        pi.PhotoName = photo.photoName;
                        pi.PhotoId = photo.photoId;
                        pi.PicPath = photo.picPath;
                        eti.listPhoto.Add(pi);
                    }
                    list.Add(eti);
                }
            }
            return list;
        }
        public ExportTaskInfo GetOneWait()
        {
            int techType = (int)TechType.ExportPhoto;
            ExportTaskInfo eti = new ExportTaskInfo();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<WorkOrderFlowList> woflList = db.WorkOrderFlowList.Where(w => w.tb_Tech.TechType == techType && w.Status == 1&&w.orderInfo.status==1).OrderByDescending(o => o.orderInfo.Priority).OrderByDescending(or => or.orderInfo.OrderProductId).ToList();
                eti.OrderCount = woflList.Count;
                WorkOrderFlowList wofl = woflList.FirstOrDefault();
                if (wofl != null)
                {
                    if (!wofl.orderInfoReference.IsLoaded)
                    {
                        wofl.orderInfoReference.Load();
                    }
                    if (!wofl.orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBagReference.Load();

                    }
                    if (!wofl.orderInfo.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBag.ShopReference.Load();
                    }
                    if (!wofl.orderInfo.photoInfo.IsLoaded)
                    {
                        wofl.orderInfo.photoInfo.Load();
                    }
                    
                    //ExportTaskInfo eti = new ExportTaskInfo();
                    eti.CtmName = wofl.orderInfo.CtmName;
                    eti.OrderBagId = eti.OrderBagid = wofl.orderInfo.Tb_OrderBag.OrderBagId;
                    eti.OrderPath = wofl.orderInfo.OrderPath;
                    eti.OrderProductName = wofl.orderInfo.OrderProductName;
                    eti.ShopName = wofl.orderInfo.Tb_OrderBag.Shop.shopName;
                    eti.PhotoNum = wofl.orderInfo.photoInfo.Count;
                    eti.OrderProductId = wofl.orderInfo.OrderProductId;
                    eti.ShopName = wofl.orderInfo.Tb_OrderBag.Shop.shopName;
                    eti.listPhoto = new List<MINERP.MODEL.FoundationModel.PhotoInfo_Info>();
                    int NoDisPhotoStatus = (int)PhotoStatus.NoDistributePrinter;
                    int DisPhotoStatus = (int)PhotoStatus.DistributedPrinter;
                    int pinbanPhotoStatus = (int)PhotoStatus.PingBan;
                    foreach (photoInfo photo in wofl.orderInfo.photoInfo.Where(p => p.status == NoDisPhotoStatus || p.status == DisPhotoStatus || p.status == pinbanPhotoStatus))
                    {
                        PhotoInfo_Info pi = new PhotoInfo_Info();
                        pi.PhotoName = photo.photoName;
                        pi.PhotoId = photo.photoId;
                        pi.PicPath = photo.picPath;
                        pi.Remark = photo.Remark;
                        eti.listPhoto.Add(pi);
                    }
                 
                }
                else
                {
                    eti = null;
                }
            }

            return eti;
        }

        public int CompleteExportPhoto(ExportTaskInfo info, int inUserid)
        {
            int rtnVal = 0;
            int techType = (int)TechType.ExportPhoto;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                orderInfo currOrderinfo = db.orderInfo.FirstOrDefault(p=>p.OrderProductId==info.OrderProductId);
                if (currOrderinfo == null)
                {
                    rtnVal = -1;//产品未找到
                }
                else
                {
                    currOrderinfo.WorkOrderFlowList.Load();
                    if (currOrderinfo.status != 1)
                    {
                        rtnVal = -2;//产品未在生产中
                    }
                    WorkOrderFlowList currWorkflow = currOrderinfo.WorkOrderFlowList.FirstOrDefault(p => p.Status == 1);
                    if (currWorkflow == null)
                    {
                        rtnVal = -2;//产品未在生产中
                    }
                    else
                    {
                        currWorkflow.tb_TechReference.Load();
                        if (currWorkflow.tb_Tech.TechType != techType)
                        {
                            rtnVal = -3;//产品流程不在导出
                        }
                        else
                        {
                            if (inUserid == 0)
                            {
                                inUserid = db.users.FirstOrDefault(p=>p.IsUseful).userID;

                            }
                            if (current.OverCurTech(currWorkflow.tb_Tech.TechId, inUserid, currOrderinfo.OrderProductId))
                            {
                                rtnVal = 1;
                            }
                            else
                            {
                                rtnVal = 0;
                            }
                        }
                    }
                }
            }


            return rtnVal;
        }

        public List<ExportTaskInfo> GetAllExportList()
        {
            int techType = (int)TechType.OutProcessPhoto;
            List<ExportTaskInfo> list = new List<ExportTaskInfo>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<WorkOrderFlowList> listwofl = db.WorkOrderFlowList.Where(w => w.tb_Tech.TechType == techType && w.Status == 1).ToList();
                foreach (WorkOrderFlowList wofl in listwofl)
                {
                    if (!wofl.orderInfoReference.IsLoaded)
                    {
                        wofl.orderInfoReference.Load();
                    }
                    if (!wofl.orderInfo.Tb_OrderBagReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBagReference.Load();

                    }
                    if (!wofl.orderInfo.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        wofl.orderInfo.Tb_OrderBag.ShopReference.Load();
                    }
                    if (!wofl.orderInfo.photoInfo.IsLoaded)
                    {
                        wofl.orderInfo.photoInfo.Load();
                    }
                    ExportTaskInfo eti = new ExportTaskInfo();
                    eti.CtmName = wofl.orderInfo.CtmName;
                    eti.OrderBagId = eti.OrderBagid = wofl.orderInfo.Tb_OrderBag.OrderBagId;
                    eti.OrderPath = wofl.orderInfo.OrderPath;
                    eti.OrderProductName = wofl.orderInfo.OrderProductName;
                    eti.ShopName = wofl.orderInfo.Tb_OrderBag.Shop.shopName;
                    eti.PhotoNum = wofl.orderInfo.photoInfo.Count;
                    eti.OrderProductId = wofl.orderInfo.OrderProductId;
                    eti.listPhoto = new List<MINERP.MODEL.FoundationModel.PhotoInfo_Info>();
                    foreach (photoInfo photo in wofl.orderInfo.photoInfo)
                    {
                        PhotoInfo_Info pi = new PhotoInfo_Info();
                        pi.PhotoName = photo.photoName;
                        pi.PhotoId = photo.photoId;
                        pi.PicPath = photo.picPath;
                        eti.listPhoto.Add(pi);
                    }
                    list.Add(eti);
                }
            }
            return list;
        }

        public List<ExportTaskInfo> CommitTaskList(List<ExportTaskInfo>  inList,int inUserid)
        {
            List<ExportTaskInfo> errorList = new List<ExportTaskInfo>();

            int techType = (int)TechType.OutProcessPhoto;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (ExportTaskInfo info in inList)
                {
                    int rtnVal = 0; ;
                    orderInfo currOrderinfo = db.orderInfo.FirstOrDefault(p => p.OrderProductId == info.OrderProductId);
                    if (currOrderinfo == null)
                    {
                        rtnVal = -1;//产品未找到
                    }
                    else
                    {
                        currOrderinfo.WorkOrderFlowList.Load();
                       
                        WorkOrderFlowList currWorkflow = currOrderinfo.WorkOrderFlowList.FirstOrDefault(p => p.Status == 1);
                       
                        if (currWorkflow == null)
                        {
                            rtnVal = -2;//产品未在生产中
                        }
                        else
                        {
                            currWorkflow.tb_TechReference.Load();
                            if (currWorkflow.tb_Tech.TechType != techType)
                            {
                                rtnVal = -3;//产品流程不在导出
                            }
                            else
                            {
                                if (inUserid == 0)
                                {
                                    inUserid = db.users.FirstOrDefault(p => p.IsUseful).userID;

                                }
                                if (current.OverCurTech(currWorkflow.tb_Tech.TechId, inUserid, currOrderinfo.OrderProductId))
                                {
                                    rtnVal = 1;
                                }
                                else
                                {
                                    rtnVal = 0;
                                }
                            }
                        }
                    }
                    if (rtnVal <= 0)
                    {
                        info.CommintResult = rtnVal;
                        errorList.Add(info);
                    }
                }
            }

            return errorList;
        }

        public List<ExportTaskInfo> ReExportTaskList(List<ExportTaskInfo> listExportTask, int userId)
        {
            List<ExportTaskInfo> errorList = new List<ExportTaskInfo>();

            int techType = (int)TechType.OutProcessPhoto;
            int techBackType = (int)TechType.ExportPhoto;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (ExportTaskInfo info in listExportTask)
                {
                    int rtnVal = 0; ;
                    orderInfo currOrderinfo = db.orderInfo.FirstOrDefault(p => p.OrderProductId == info.OrderProductId);
                    if (currOrderinfo == null)
                    {
                        rtnVal = -1;//产品未找到
                       
                    }
                    else
                    {
                        currOrderinfo.WorkOrderFlowList.Load();
                        currOrderinfo.WorkOrderFlowList.ToList().ForEach(ea => ea.tb_TechReference.Load());
                        WorkOrderFlowList currWorkflow = currOrderinfo.WorkOrderFlowList.FirstOrDefault(p => p.Status == 1);
                        WorkOrderFlowList backWorkflow = currOrderinfo.WorkOrderFlowList.FirstOrDefault(p => p.tb_Tech.TechType == techBackType);
                        if (currWorkflow == null)
                        {
                            rtnVal = -2;//产品未在生产中
                            
                        }
                        else
                        {
                            currWorkflow.tb_TechReference.Load();
                            if (currWorkflow.tb_Tech.TechType != techType)
                            {
                                rtnVal = -3;//产品流程不在导出
                                
                            }
                            else
                            {
                                currWorkflow.Status = 0;
                                currWorkflow.UserId = 0;
                                backWorkflow.Status = 1;
                                backWorkflow.UserId = 0;
                                currOrderinfo.photoInfo.Load();
                                int NoDisPhotoStatus = (int)PhotoStatus.NoDistributePrinter;
                                currOrderinfo.photoInfo.ToList().ForEach(each => each.status = NoDisPhotoStatus);
                            }
                            if (rtnVal < 0)
                            {
                                info.CommintResult = rtnVal;
                                errorList.Add(info);
                            }
                        }
                    }
                    
                }
                db.SaveChanges();
            }

            return errorList;
        }
    }
}
